/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package co.edu.sd.jxta;

import co.edu.sd.jxta.socket.P2PClientSoccket;
import java.util.Enumeration;
import net.jxta.discovery.DiscoveryEvent;
import net.jxta.discovery.DiscoveryListener;
import net.jxta.document.Advertisement;
import net.jxta.peergroup.PeerGroup;
import net.jxta.protocol.DiscoveryResponseMsg;
import net.jxta.protocol.PipeAdvertisement;

/**
 *
 * @author SD
 */
public class AsynchronousTask implements DiscoveryListener {

    private PeerGroup oPeerGroup;
    private IPeer peer;
    private Command cmd;
    private IPeerUI peerUI;

    public AsynchronousTask(PeerGroup oPeerGroup, IPeer peer, Command cmd, IPeerUI peerUI) {
        this.peerUI = peerUI;
        this.oPeerGroup = oPeerGroup;
        this.peer = peer;
        this.cmd = cmd;
    }

    public void discoveryEvent(DiscoveryEvent ev) {
        DiscoveryResponseMsg res = ev.getResponse();
        peerUI.append(" [  Got a Discovery Response [" + res.getResponseCount() + " elements]  from peer : " + ev.getSource() + "  ] ");

        Advertisement adv;
        Enumeration en = res.getAdvertisements();

        if (en != null) {
            while (en.hasMoreElements()) {
                adv = (Advertisement) en.nextElement();
                if (adv instanceof PipeAdvertisement) {
                    PipeAdvertisement pipeAdv = (PipeAdvertisement) adv;
                    new P2PClientSoccket(pipeAdv, oPeerGroup, peer, cmd, peerUI);
                }

            }
        }
    }
}
